import VueRouter from 'vue-router'
import Toast from './components/widgets/toast'
import { domain } from './constants'
import Vue from 'vue'

export const base = process.env.NODE_ENV === 'production' ? '/h5/' : '/'

Vue.use(VueRouter)

// 是否在APP中打开
export const android = navigator.userAgent.indexOf('Android') >= 0
export const app = navigator.userAgent.indexOf('LotteryApp') >= 0
Vue.prototype.app = app
if (app) {
  document.documentElement.classList.add('app')
}

export function back () {
  if (app) {
    return location.href = 'redirect://back'
  }
  router.back()
}

export function jump (to) {
  if (app) {
    return location.href = 'redirect:/' + to
  }
  router.push(to)
}

export function redirect (url, type = 1) {

  Toast(`redirect://webview?url=${encodeURIComponent(url)}&type=${type}`)
  if (app) {
    return location.href = `redirect://webview?url=${encodeURIComponent(url)}&type=${type}`
  }
}

export function faq (view, cb) {
  var url = domain + view + '.html'
  redirect(url, 1)
  cb && cb()
}

export function invoke (action) {
  if (app) {
    return location.href = 'invoke://' + action
  }
}

const routes = [
  {
    path: '/', component: () => import(/* webpackChunkName: "index" */ './components/app'),
    children: [
      {path: '', name: 'service', component: () => import(/* webpackChunkName: "index" */ './components/index')},
      {path: 'info', name: 'info', component: () => import(/* webpackChunkName: "index" */ './components/info')},
      {path: 'prize', name: 'prize', component: () => import(/* webpackChunkName: "index" */ './components/prize')},
      {path: 'mine', name: 'mine', component: () => import(/* webpackChunkName: "index" */ './components/mine')}
    ]
  },
  {path: '/jingcai', meta:{title: '竞彩足球'}, component: () => import(/* webpackChunkName: "jingcai" */ './components/jingcai')},
  {path: '/jingcai/prizes', component: () => import(/* webpackChunkName: "jingcai" */ './components/jingcai/prizes')},
  {path: '/jingcai/cart', component: () => import(/* webpackChunkName: "jingcai" */ './components/jingcai/cart')},
  {path: '/article', component: () => import(/* webpackChunkName: "article" */ './components/article')},
  {path: '/match', component: () => import(/* webpackChunkName: "match" */ './components/match')},
  {path: '/transactions', component: () => import(/* webpackChunkName: "transactions" */ './components/orders/transactions')},
  {path: '/orders', component: () => import(/* webpackChunkName: "tickets" */ './components/orders')},
  {path: '/tickets', component: () => import(/* webpackChunkName: "tickets" */ './components/orders/tickets')},
  {path: '/winlose/order', component: () => import(/* webpackChunkName: "winlose" */ './components/winlose/order')},
  {path: '/winlose/prizes', component: () => import(/* webpackChunkName: "winlose" */ './components/winlose/prizes')},
  {path: '/winlose/cart', component: () => import(/* webpackChunkName: "winlose" */ './components/winlose/cart')},
  {path: '/winlose', meta:{title: '胜负彩'}, component: () => import(/* webpackChunkName: "winlose" */ './components/winlose')},
  {path: '/daletou', meta:{title: '大乐透'}, component: () => import(/* webpackChunkName: "daletou" */ './components/daletou')},
  {path: '/daletou/prizes', component: () => import(/* webpackChunkName: "daletou" */ './components/daletou/prizes')},
  {path: '/daletou/prize', component: () => import(/* webpackChunkName: "daletou" */ './components/daletou/prize')},
  {path: '/daletou/order', component: () => import(/* webpackChunkName: "daletou" */ './components/daletou/order')},
  {path: '/daletou/cart', component: () => import(/* webpackChunkName: "daletou" */ './components/daletou/cart')},
  {path: '/hubei11c5', meta:{title: '华11选5'}, component: () => import(/* webpackChunkName: "hubei11c5" */ './components/hubei11c5')},
  {path: '/hubei11c5/trends', component: () => import(/* webpackChunkName: "hubei11c5" */ './components/hubei11c5/trends')},
  {path: '/hubei11c5/prizes', component: () => import(/* webpackChunkName: "hubei11c5" */ './components/hubei11c5/prizes')},
  {path: '/hubei11c5/prize', component: () => import(/* webpackChunkName: "hubei11c5" */ './components/hubei11c5/prize')},
  {path: '/hubei11c5/order', component: () => import(/* webpackChunkName: "hubei11c5" */ './components/hubei11c5/order')},
  {path: '/hubei11c5/cart', component: () => import(/* webpackChunkName: "hubei11c5" */ './components/hubei11c5/cart')},
  {path: '/iframe', component: () => import(/* webpackChunkName: "iframe" */ './components/widgets/frame')},
  {path: '/login', component: () => import(/* webpackChunkName: "login" */ './components/login')}
]

// scrollBehavior:
// - only available in html5 history mode
// - defaults to no scroll behavior
// - return false to prevent scroll
const scrollBehavior = (to, from, savedPosition) => {
  console.log(savedPosition)
  if (savedPosition) {
    // savedPosition is only available for popstate navigations.
    return savedPosition
  } else {
    const position = {}
    // new navigation.
    // scroll to anchor by returning the selector
    if (to.hash) {
      position.selector = to.hash
    }
    // check if any matched route config has meta that requires scrolling to top
    if (to.matched.some(m => m.meta.scrollToTop)) {
      // cords will be used if no selector is provided,
      // or if the selector didn't match any element.
      position.x = 0
      position.y = 0
    }
    // if the returned position is falsy or an empty object,
    // will retain current scroll position.
    return position
  }
}

var router = new VueRouter({
  linkActiveClass: 'active',
  mode: 'history',
  routes,
  base
})

router.beforeEach(function (to, from, next) {
  if (to.path != from.path) {
    Toast.loading('加载中...')
  }
  next()
})

router.afterEach(function (to) {
  var title = to.meta.title
  if (to.query.rule == 'r9') {
    title = '任选9场'
  }
  if (title) {
    document.title = title
    router.app.$http.post('/addH5Statistics', { link: location.href, title})
  }
  setTimeout(() => {
    Toast.clear()
  }, 500)
})

export default router

